
import { useAuthStoreHook } from '@/store/auth'
import { isWriteList } from './isWriteList'
import { NavigationGuardNext, RouteLocationNormalized, Router } from 'vue-router'

function routerBeforeEach(to: RouteLocationNormalized, _from: RouteLocationNormalized, next: NavigationGuardNext) {
    // to and from are both route objects. must call `next`.
    const authStore = useAuthStoreHook()
    // console.log('路由守卫', to.fullPath, _from.fullPath)
    // 判断是否登录,如果么有登录
    if(!authStore.hasToken){
        if(isWriteList(to.path)) return next()
        return next('/login')
    }
    if(authStore.hasToken && to.path == '/login') return next('/')
    next()
}


export function AddRouterBeforeEach(router:Router){
    router.beforeEach(routerBeforeEach)
}